package hczc.longcai.com.hccrowdfunding.home.first_index;


import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import java.util.ArrayList;
import java.util.List;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.home.first_index.adapters.IndexHotAdapter;
import hczc.longcai.com.hccrowdfunding.home.first_index.beans.IndexHotRecommend;
import hczc.longcai.com.hccrowdfunding.home.first_index.beans.ProjectInfo;
import hczc.longcai.com.hccrowdfunding.home.first_index.fragments.BottomFragment;
import hczc.longcai.com.hccrowdfunding.home.first_index.fragments.InformFragment;
import hczc.longcai.com.hccrowdfunding.home.first_index.fragments.LabelFragment;
import hczc.longcai.com.hccrowdfunding.home.first_index.fragments.SlideFragment;
import hczc.longcai.com.hccrowdfunding.utils.bitmaputils.ImageLoader;
import hczc.longcai.com.hccrowdfunding.views.IndexListView;

/**
 * A simple {@link Fragment} subclass.
 */
public class FirstFragment extends Fragment {


    private List<ProjectInfo> list;
    private ScrollView scrollView;

    private IndexListView listView;
    private IndexHotAdapter adapter;


    private SlideFragment slideFragment;
    private LabelFragment labelFragment;
    private InformFragment informFragment;
    private BottomFragment bottomFragment;

    private FragmentManager manager;
    private FragmentTransaction transaction;

    private ProInfoAsyncTask asyncTask;
    private ImageLoader imageLoader;

    public FirstFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imageLoader = BaseApplication.imageLoader;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, null);
        BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
        initDatas();
        initViews(view);

        return view;
    }

    private void initDatas() {
        asyncTask = new ProInfoAsyncTask();
        asyncTask.execute(((Void) null));
    }

    private void initViews(View view) {

        initFragments();

        scrollView = ((ScrollView) view.findViewById(R.id.index_scrollview));
        scrollView.smoothScrollTo(0, 0);
        listView = ((IndexListView) view.findViewById(R.id.index_listview));
    }

    private void initFragments() {

        manager = getActivity().getSupportFragmentManager();
        transaction = manager.beginTransaction();

        slideFragment = new SlideFragment();
        labelFragment = new LabelFragment();
        informFragment = new InformFragment();
        bottomFragment = new BottomFragment();

        transaction.replace(R.id.index_framelayout_slide, slideFragment);
        transaction.replace(R.id.index_framelayout_lable, labelFragment);
        transaction.replace(R.id.index_framelayout_inform, informFragment);
        transaction.replace(R.id.index_framelayout_bottom, bottomFragment);

        transaction.commit();
    }

    public void removeFocus() {
        scrollView.smoothScrollTo(0, 0);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    class ProInfoAsyncTask extends AsyncTask<Void, Void, List<ProjectInfo>> {


        @Override
        protected List<ProjectInfo> doInBackground(Void... params) {
            List<ProjectInfo> proinfo = BaseApplication.dbIndexController.getProinfo();
            return proinfo;
        }

        @Override
        protected void onPostExecute(List<ProjectInfo> list) {

            super.onPostExecute(list);
            adapter = new IndexHotAdapter(getActivity(), list, imageLoader);
            listView.setAdapter(adapter);

        }
    }
}
